package org.example.thread4;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 普通线程调用 - 给线程命名
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2020/10/31 16:34
 */
public class MyThread extends Thread {

    @Override
    public synchronized void run() {
        try {
            System.out.println(new Date() + " 任务开始: " + Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(1 + new Random().nextInt(3));
            System.out.println(new Date() + " 任务结束：" + Thread.currentThread().getName());
            //......
            //Sat Oct 31 16:34:51 CST 2020 任务开始: 武当梯云纵线程3
            //Sat Oct 31 16:34:51 CST 2020 任务开始: 武当梯云纵线程4
            //Sat Oct 31 16:34:52 CST 2020 任务结束：武当梯云纵线程3
            //Sat Oct 31 16:34:52 CST 2020 任务结束：武当梯云纵线程0
            //......
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            MyThread myThread = new MyThread();
            /**直接使用Thread的setName方法即可命名*/
            myThread.setName("武当梯云纵线程" + i);
            myThread.start();
        }
    }
}